//
//  ViewController.m
//  NSThread
//
//  Created by 杨康 on 2017/12/5.
//  Copyright © 2017年 杨康. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //线程初始化方法
    NSThread *thread=[[NSThread alloc]init];
    NSThread *thread2=[[NSThread alloc]initWithBlock:^{
        
    }];
    NSThread *thread3=[[NSThread alloc]initWithTarget:self selector:@selector(demo:) object:@"yang"];
    
    //这两个方法是便利构造类方法，用它们创建的线程会立马执行不用start
    [NSThread detachNewThreadWithBlock:^{
        //10.0后
        
    }];
    [NSThread detachNewThreadSelector:@selector(demo:) toTarget:self withObject:@"detach"];
    //在后台运行，调用该方法会自动创建一个异步线程
    [self performSelectorInBackground:@selector(demo:) withObject:@"kang"];
    
    
    //手动开始
    [thread start];
    
    //手动取消 (做了标记）
    [thread cancel];
    
    
    //退出当前线程
    [NSThread exit];
    
    
    
    
    //是否是主线程 一个是类方法，一个是实例方法
    BOOL a=[thread isMainThread];
    BOOL b=[NSThread isMainThread];
    
    //正在执行
    BOOL c=thread.executing;
    //已经完成
    BOOL d=thread.finished;
    //已经取消
    BOOL e=thread.cancelled;
    
    
    //当前是否是多线程
    [NSThread isMultiThreaded];
    
    //当前线程的优先级
    double priority=[NSThread threadPriority];
    //设置当前线程的优先级
    [NSThread setThreadPriority:priority];
    
    //线程优先级
    NSLog(@"%f",thread.threadPriority);
    //线程名字
    NSLog(@"%@",thread.name);
    
    
    
    
    //获取当前线程
    [NSThread currentThread];
    
    //获取主线程
    [NSThread mainThread];
    
    
    
    //当前线程睡眠
    [NSThread sleepUntilDate:[NSDate date]];
    [NSThread sleepForTimeInterval:1.0];
    
    
    
    
    //回到主线程执行操作的方法
    [self performSelectorOnMainThread:@selector(demo:) withObject:@"kang" waitUntilDone:YES];
    [self performSelectorOnMainThread:@selector(demo:) withObject:@"kang" waitUntilDone:YES modes:@[@"yang"]];
     // equivalent to the first method with kCFRunLoopCommonModes
    
    
    //回到线程执行操作的方法
    [self performSelector:@selector(demo:) onThread:thread withObject:@"kang" waitUntilDone:YES];
    [self performSelector:@selector(demo:) onThread:thread withObject:@"kang" waitUntilDone:YES modes:@[@"yang"]];
    // equivalent to the first method with kCFRunLoopCommonModes
    
    
    //入口函数，如果你要重写thread就得override，当然，我这里没贴上的api重写thread应该能用到。不过重写这种骚操作基本不会用到。
    // thread body method
//    - (void)main API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
    
    //几个通知，看命名就知道意思了
    FOUNDATION_EXPORT NSNotificationName const NSWillBecomeMultiThreadedNotification;
    FOUNDATION_EXPORT NSNotificationName const NSDidBecomeSingleThreadedNotification;
    FOUNDATION_EXPORT NSNotificationName const NSThreadWillExitNotification;
    
    
    
    
    
    
    
}


- (void)demo:(id)obj
{
    NSLog(@"传入参数 => %@",obj);
    
}


@end
